﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;

namespace PraxisRent.UI
{
    public partial class FrmMttoPago : Form
    {
        [Dependency]
        public IPagoBL pagoBL { get; set; }

        [Dependency]
        public ICreditoBL creditoBL { get; set; }

        [Dependency]
        public IAlquilerBL alquiBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJuriBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNatBL { get; set; }

        public FrmMttoPago()
        {
            InitializeComponent();
            this.BuilUp();
        }

        public PAGO _currentPago { get; set; }

        public void pagarPago()
        {
            pAGOBindingSource.DataSource = _currentPago;
            DateTime date = _currentPago.FechaLimite ?? default(DateTime);
            lblFecVencimiento.Text = Convert.ToString(_currentPago.FechaLimite.Value.ToString("dd/MM/yyyy"));
            //Convert.ToString(date.Day) + "/" + Convert.ToString(date.Month) + "/" + Convert.ToString(date.Year);
            /////// OBTENIENDO LOS NOMBRES DE LOS CLIENTES  //////////

            var alquiler = alquiBL.GetAllFromAlquilerByCodigo(_currentPago.CREDITO.CodigoAlquiler);
            var cliNat = (from c in cliNatBL.GetAllFromClienteNatural()
                          where c.ClienteNaturalId.Equals(alquiler.CodigoCliente)
                          select c).SingleOrDefault();
            var cliJuri = (from c in cliJuriBL.GetAllFromClienteJuridico()
                           where c.ClienteJuridicoId.Equals(alquiler.CodigoCliente)
                           select c).SingleOrDefault();

            ///// ASIGNAMOS EL CLIENTE A LOS LABEL´S /////////

            if (cliNat != null)
            {
                lblCliente.Text = cliNat.NombreCompleto;
            }
            else if (cliJuri != null)
            {
                lblCliente.Text = cliJuri.RazonSocial;
            }
        }

        private void FrmMttoPago_Load(object sender, EventArgs e)
        {

        }

        private void abonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void numComprobanteTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");


            string cadena = punto + guion;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void abonoTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (Convert.ToDecimal(abonoTextBox.Text) < _currentPago.Abono)
                {
                    MessageBox.Show("Abono debe ser mayor a: " + _currentPago.Abono, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    abonoTextBox.Text = Convert.ToString(_currentPago.Abono);
                }
            }
        }

        private void abonoTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if (Convert.ToDecimal(abonoTextBox.Text) < _currentPago.Abono)
                {
                    MessageBox.Show("Abono debe ser mayor a: " + _currentPago.Abono, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    abonoTextBox.Text = Convert.ToString(_currentPago.Abono);
                }
            }
        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            var pago = pAGOBindingSource.Current as PAGO;

            var msg = "ERROR";
            if (pago != null)
            {
                try
                {
                    pago.Descripcion = descripcionTextBox.Text.ToUpper();
                    pago.NumComprobante = numComprobanteTextBox.Text.ToUpper();
                    pago.Cancelado = true;
                    pago.FechaPago = DateTime.Now;
                    pagoBL.UpdatePago(pago);

                    //// cambiamos el estado CANCELADO del CREDITO
                    var pagosCancelados = (from c in pagoBL.GetAllFromPago()
                                           where c.Cancelado == false
                                           select c).ToList();

                    if (pagosCancelados.Count == 0)
                    {
                        var credito = creditoBL.GetAllFromCreditoById(pago.IdCredito);
                        credito.Cancelado = true;
                        creditoBL.UpdateCredito(credito);
                    }

                    MessageBox.Show("Registro Correcto ", "Registro",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
